<?php

use App\Http\Resources\HomeResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\CoinsApplicationController;
use App\Http\Controllers\Admin\CoinsCodingController;
use App\Http\Services\Admin\CoinsApplicationService;
use App\Http\Controllers\Admin\Account\CommonCoinCoinController;
use App\Http\Controllers\Admin\Account\CommonCoinCoinLogController;
use App\Http\Controllers\Admin\Account\CommonCoinReserveCoinLogController;
use App\Http\Controllers\Admin\ExceptionController;
use App\Http\Controllers\Admin\CreditsAdminOplogController;
use App\Http\Controllers\Admin\CoinsConfigController;
use App\Http\Controllers\Admin\JobsController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

Route::group([
    'namespace' => 'Admin',
    #'middleware'=>'staff.auth'
], function () {

    //应用管理
    Route::prefix('staff')->group(function () {
        Route::prefix('application')->group(function () {
            Route::get('list', [CoinsApplicationController::class, 'list'])->name('coins-application.list');
            Route::get('detail', [CoinsApplicationController::class, 'detail'])->name('coins-application.detail');
            Route::post('create', [CoinsApplicationController::class, 'create'])->name('coins-application.create');
            Route::post('info/update', [CoinsApplicationController::class, 'update'])->name('coins-application.update');
            Route::post('open', [CoinsApplicationController::class, 'open'])->name('coins-application.open');
            Route::post('close', [CoinsApplicationController::class, 'close'])->name('coins-application.close');
            Route::post('manual/switch', [CoinsApplicationController::class, 'is_manual'])->name('coins-application.is_manual');
            Route::post('settle/withhold/switch', [CoinsApplicationController::class, 'is_deduct'])->name('coins-application.is_deduct');
            Route::post('settle/verify/type', [CoinsApplicationController::class, 'verify_type'])->name('coins-application.verify_type');
            Route::post('settle/rule/update', [CoinsApplicationController::class, 'rule_update'])->name('coins-application.rule_update');
            Route::get('verify/type/list', function (CoinsApplicationService $service) {
                return HomeResource::success($service->getVerificationMode());
            })->name('coins-application.verificationmode');
            Route::get('settle/rule/list', function (CoinsApplicationService $service) {
                return HomeResource::success($service->getClosingRule());
            })->name('coins-application.closingrule');
        });

        //编码管理
        Route::prefix('coin/type')->group(function () {
            Route::get('list', [CoinsCodingController::class, 'list']);
            Route::get('detail', [CoinsCodingController::class, 'detail']);
            Route::post('create', [CoinsCodingController::class, 'create']);
            Route::post('onoff', [CoinsCodingController::class, 'onoff']);
            //二级红包编码管理
            Route::prefix('junior')->group(function () {
                Route::get('list/{parent_code}', [CoinsCodingController::class, 'junior_list']);
                Route::post('create', [CoinsCodingController::class, 'junior_create']);
                Route::post('update', [CoinsCodingController::class, 'junior_update']);
                Route::post('onoff', [CoinsCodingController::class, 'junior_onoff']);
            });
        });

        //预扣记录
        Route::prefix('coin')->group(function () {
            Route::prefix('reserve')->group(function () {
                Route::get('list', [CommonCoinReserveCoinLogController::class, 'list']);
            });
        });

        //账户管理
        Route::prefix('account')->group(function () {
            //红包账户
            Route::get('list', [CommonCoinCoinController::class, 'list']);
            Route::get('detail', [CommonCoinCoinController::class, 'detail']);
            //收支记录
            Route::prefix('income/expenses')->group(function () {
                Route::get('list', [CommonCoinCoinLogController::class, 'list']);
                Route::get('detail', [CommonCoinCoinLogController::class, 'detail']);
            });
        });

        //异常管理
        Route::prefix('exception')->group(function () {
            Route::get('list', [ExceptionController::class, 'list']);
            Route::get('detail', [ExceptionController::class, 'detail']);
        });

        //操作记录
        Route::prefix('operation/log')->group(function () {
            Route::get('list', [CreditsAdminOplogController::class, 'list']);
            Route::get('detail', [CreditsAdminOplogController::class, 'detail']);
        });

        //管理设置
        Route::prefix('config')->group(function () {
            Route::get('detail', [CoinsConfigController::class, 'detail']);
            Route::post('update', [CoinsConfigController::class, 'update']);
        });

        //JOBS设置
        Route::prefix('queue')->group(function () {
            Route::get('statistics', [JobsController::class, 'statistics']);
            Route::get('pending/list', [JobsController::class, 'running']);
            Route::get('pending/retry/list', [JobsController::class, 'retrying']);
            Route::get('failed/list', [JobsController::class, 'failed']);
        });
    });
});
